

/**
 * @returns A capitalized string (e.g. "some test sentence" becomes "Some test sentence")
 * @type String
 */
String.prototype.capitalize = function() {
	return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase();
};

/**
 * @returns The string in Title Case (e.g. "some test sentence" becomes "Some Test Sentence")
 * @type String
 */
String.prototype.titleize = function() {
	return this.replace(/\w\S*/g, function(txt) {
				return txt.charAt(0).toUpperCase()
						+ txt.substr(1).toLowerCase();
			});
};

/**
 * Takes any string and de-underscores and uppercases it
 * e.g. long_underscored_string => LongUnderscoredString
 */
String.prototype.camelize = function() {
	return this.replace(/_/g, " ").titleize().replace(/ /g, "");
};

String.prototype.underscore = function() {
	return this.toLowerCase().replace(/ /g, "_");
};

String.prototype.singularize = function() {
	return ExtMVC.Inflector.singularize(this);
};

String.prototype.pluralize = function() {
	return ExtMVC.Inflector.pluralize(this);
};

String.prototype.escapeHTML = function() {
	return this.replace(/&/g, '&amp;').replace(/>/g, '&gt;').replace(/</g,
			'&lt;'
	).replace(/"/g, '&quot;');
};

/**
 * Converts this string into a currency, prepended with the given currency symbol
 * @param {String} symbol The currency symbol to use (defaults to $)
 */
String.prototype.toCurrency = function(symbol) {
	if (typeof(symbol) == 'undefined') {
		var symbol = '$';
	}

	var beforeDecimal = this.split(".")[0], afterDecimal = this.split(".")[1];

	var segmentCount = Math.floor(beforeDecimal.length / 3);
	var firstSegmentWidth = beforeDecimal.length % 3, pointerPosition = firstSegmentWidth;

	var segments = firstSegmentWidth == 0 ? [] : [beforeDecimal.substr(0,
			firstSegmentWidth
	)];

	for (var i = 0; i < segmentCount; i++) {
		segments.push(beforeDecimal.substr(firstSegmentWidth + (i * 3), 3));
	};

	beforeDecimal = symbol + segments.join(",");

	return afterDecimal
			? String.format("{0}.{1}", beforeDecimal, afterDecimal)
			: beforeDecimal;
};

Ext.apply(String.prototype, {
			truncate_length	: 25,

			truncate_string	: "...",

			truncate		: function(truncate_length, truncate_string) {
				truncate_length = truncate_length || this.truncate_length;
				truncate_string = truncate_string || this.truncate_string;
				var length = truncate_length - truncate_string.length;
				return this.length > truncate_length ? this.substr(0, length)
						+ truncate_string : String(this);
			}
		});